import * as React from "react";
import { Business } from '../../hook/businessList';
import { InfoItem } from './cgi';

import getLineChartOption from "./calc";
import Chart from '../chart';
import Search from "antd/lib/transfer/search";

export type ChartsProps = {
    items: InfoItem[],
    list: Business[],
    selectId: number
}

export default function ChartsWarpper(props: ChartsProps) {
    const id2name = {};
    props.list.forEach(project => {
        id2name[project.id.toString()] = project.name;
    })
    const projectDic = {};
    const xAxisData = new Set([]);
    const unActiveProjects = new Set((localStorage.getItem('unActiveProjects') || '').split(','));
    props.items.length > 0 && props.items.forEach(item => {
        const projectId = item.projectId.toString();
        if (props.selectId === -1 && unActiveProjects.has(projectId)) {
            return;
        };
        if (projectDic[projectId]) {
            projectDic[projectId].data = [...projectDic[projectId].data, item.total];
            projectDic[projectId].name = id2name[projectId];
        } else {
            projectDic[projectId] = {
                data: [item.total],
                name: ''
            };
        }
        if (xAxisData.has(item.endDate)) {
            return;
        } else {
            xAxisData.add(item.endDate);
        }
    });
    let xAxisDataArray = Array.from(xAxisData);
    xAxisDataArray = xAxisDataArray.map(date => new Date(date).toLocaleDateString('chinese', { hour12: false }));
    let series = [];
    for (const projectId in projectDic) {
        series.push({
            name: projectDic[projectId].name,
            data: projectDic[projectId].data
        });
    }
    const option = getLineChartOption('统计图表', xAxisDataArray, series);
    return (
        <div>
            <Chart option={option} styleName={'index-chart'}></Chart>
        </div>
    )


}
